package server.webEE.bean;

import java.math.BigInteger;
import java.rmi.RemoteException;
import java.util.Properties;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.transaction.SystemException;

import test.bean.TestEntityHomeLocal;
import test.bean.TestEntityLocal;
import weblogic.logging.NonCatalogLogger;

public class TestSessionFulBean implements SessionBean {

	private static final NonCatalogLogger LOG = new NonCatalogLogger(
			TestSessionFulBean.class.getName());
	/**
	 * 
	 */
	private static final long serialVersionUID = -899118395746418738L;

	private SessionContext sctx;

	public void ejbCreate() {

		LOG.error("ejbCreateFULL");
	}

	public String myLog() {
		LOG.error("myLog_start-FULL");
		Context context;
		TestEntityLocal local = null;
		try {
			context = new InitialContext();
			Object object = context
					.lookup("test/TestEntityHomeLocal");
			TestEntityHomeLocal homeLocal = (TestEntityHomeLocal) PortableRemoteObject
					.narrow(object, TestEntityHomeLocal.class);
			local=(TestEntityLocal) homeLocal.findByPrimaryKey(new BigInteger("9000"));
			LOG.error("myLog_end-FULL");
			return local.getEcho();
		} catch (NamingException e) {
			throw new EJBException();
		} catch (FinderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return local.getEcho();
	}
	
	public String myTransaction() {
		Context context;
		TestEntityLocal local = null;
		try {
			sctx.getUserTransaction().begin();
			LOG.error("myLog_FULL-begin");
			
			context = new InitialContext();
			Object object = context
					.lookup("test/TestEntityHomeLocal");
			TestEntityHomeLocal homeLocal = (TestEntityHomeLocal) PortableRemoteObject
					.narrow(object, TestEntityHomeLocal.class);
			local=(TestEntityLocal) homeLocal.findByPrimaryKey(new BigInteger("4"));
			
			sctx.getUserTransaction().commit();
			LOG.error("myLog_FULL-commit");
			return local.getEcho();
		} catch (NamingException e) {
			throw new EJBException();
		} catch (FinderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable t) {
			try {
				sctx.getUserTransaction().rollback();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return local.getEcho();
	}

	public javax.naming.Context getInitialContext() throws NamingException {
		LOG.error("getInitialContextFULL");
		return new InitialContext(new Properties());
	}

	public void ejbActivate() throws EJBException, RemoteException {
		LOG.error("ejbActivateFULL");

	}

	public void ejbPassivate() throws EJBException, RemoteException {
		LOG.error("ejbPassivateFULL");

	}

	public void ejbRemove() throws EJBException, RemoteException {
		LOG.error("ejbRemoveFULL");

	}

	public void setSessionContext(SessionContext ctx) throws EJBException,
			RemoteException {
		this.sctx = ctx;
		LOG.error("setSessionContextFULL");

	}

}
